<template>
    <div>
        登录人:{{ usestore.userInfo.userName }}
        <h1>用药列表</h1>
        医生名称:
        <input type="text" v-model="info.name">
        科室:
        <select v-model="info.deparid">
            <option value="">请选择</option>
            <option v-for="item in lists" :value="item.id">{{ item.departmentName }}</option>
        </select>
        <input type="button" value="查询" @click="show" class="btn btn-success">
        <input type="button" value="批量完成" @click="updatestate" class="btn btn-info">
        <input type="button" value="导出到Excel" @click="exportexce" class="btn btn-warning">
        <table class="table table-bordered">
           <tbody>
             <tr>
               <td><input type="checkbox" v-model="checked" @change="updall"></td>
               <td>编号</td>
               <td>药品名称</td>
               <td>科室</td>
               <td>用药日期</td>
               <td>病人名称</td>
               <td>病房号码</td>
               <td>用药状态</td>
               <td>配药医生</td>
               <td>操作</td>
            </tr>
            <tr v-for="item in data.list" :key="item.id">
               <td><input type="checkbox" v-model="ids" @change="notall" :value="item.id"></td>
               <td>{{moment( new Date).format('YYYYMMDD')+Math.random().toString().substring(2,5) }}</td>
               <td>{{item.drugName}}</td>
               <td>{{item.departmentName}}</td>
               <td>{{ moment(item.drugTime).format('YYYY-MM-DD') }}</td>
               <td>{{item.people}}</td>
               <td>{{item.drugHouse}}</td>
               <td>{{item.drugState==1?'未完成':'已完成'}}</td>
               <td>{{item.drugPeople}}</td>
               <td>
                <input type="button" value="修改" @click="upd(item.id)" class="btn btn-primary">
                <input type="button" value="删除" @click="del(item.id)" class="btn btn-danger">
               </td>
            </tr>
           </tbody>
        </table>
        <div>
            <span>共{{ data.totalCount }}条</span>
            <span>每页{{ info.pagesize }}条</span>
            <span>当前第{{ info.pageindex }}/{{ data.totalPage }}页</span>
            <input type="button" value="首页" @click="page(1)">
            <input type="button" value="上一页" @click="page(info.pageindex-1)">
            <input type="button" v-for="item in data.totalPage" :value="item" @click="page(item)">
            <input type="button" value="下一页" @click="page(info.pageindex+1)">
            <input type="button" value="尾页" @click="page(data.totalPage)">
            <input type="number" v-model="info.pageindex" @change="show">
            <input type="button" value="跳转" @click="page(info.pageindex)">
            <select v-model="info.pagesize" @click="show">
                <option v-for="item in arr" :value="item">每页{{ item }}条</option>
            </select>
        </div>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from "vue";
import axios, { all } from "axios";
import moment from "moment";
import { useStore } from "@/stores/user";
import {useRouter,useRoute} from "vue-router";
const router=useRouter();
const route=useRoute();
const usestore=useStore();
onMounted(()=>
{
    if(usestore.userInfo.userId)
    {
        show1();
        show();
    }
    else
    {
        router.push('/moneylogin');
    }
})
let info:any=reactive({
  "name": "",
  "deparid": "",
  "pageindex": 1,
  "pagesize": 2
})
let data:any=reactive({
  "totalCount": 0,
  "totalPage": 0,
  "list":[]
})
const page=(num:any)=>
{
    if(num<1)
    {
        num=1;
    }
    if(num>data.totalPage)
    {
        num=data.totalPage;
    }
    info.pageindex=num;
    show();
}
let arr:any=ref([2,3,5,7]);
const del=(id:any)=>
{
    var f=confirm("确认删除吗?");
    if(f)
    {
        axios({
        url:'http://localhost:5182/api/Money/DelDrug',
        method:'put',
        params:
        {
            id:id
        }
    })
    .then((res)=>
    {
        console.log(res);
        if(res.data>0)
        {
            alert("删除成功");
            
            show();
            
            return;
        }
        else
        {
            alert("删除失败");
            return;
        }
    })
    .catch((err)=>
    {
        console.log(err);
    })
    }
}
let checked:any=ref(false);
let ids:any=ref([]);
const updall=()=>
{
    console.log(checked.value);
    if(checked.value)
    {
        ids.value=data.list.map((item:any)=>
        {
            return item.id;
        })
    }
    else
    {
        ids.value=[];
    }
}
const notall=()=>
{
    if(ids.value.length==data.list.length)
    {
        checked.value=true;
    }
    else
    {
        checked.value=false;
    }
}
const updatestate=()=>
{
    if(ids.value.length==0)
    {
        alert("必选一项");
        return;
    }
    var f=confirm("确认修改吗?");
    if(f)
    {
        axios({
        url:'http://localhost:5182/api/Money/UpdateState',
        method:'put',
        params:
        {
            ids:ids.value.toString()
        }
    })
    .then((res)=>
    {
        console.log(res);
        if(res.data>0)
        {
            alert("修改成功");
            ids.value=[];
            checked.value=false;
            show();
            return;
        }
        else
        {
            alert("修改失败");
            return;
        }
    })
    .catch((err)=>
    {
        console.log(err);
    })
    }
}
const exportexce=()=>
{
   
    location.href=`http://localhost:5182/api/Money/ExportExcel?pageindex=1&pagesize=5`;
}
const show=()=>
{
    
    axios({
        url:'http://localhost:5182/api/Money/ShowDrug',
        method:'get',
        params:info
    })
    .then((res)=>
    {
        console.log(res);
        Object.assign(data,res.data);
    })
    .catch((err)=>
    {
        console.log(err);
    })
}
let info1:any=reactive({
  "departmentname": ""
})
let lists:any=ref([]);
const show1=()=>
{
    
    axios({
        url:'http://localhost:5182/api/Money/ShowDepartment',
        method:'get',
        params:info1
    })
    .then((res)=>
    {
        console.log(res);
        lists.value=res.data;
    })
    .catch((err)=>
    {
        console.log(err);
    })
}
const upd=(id:any)=>
{
    router.push({
        path:'/upddrug',
        query:{
            id:id
        }
    })
}
</script>

<style scoped>
span{
    margin: 0 5px;
}
</style>